// 新增/修改页面使用
var seq = 1; // 全局变量
// 新增行
function addLine(page) {
	var tbl = document.getElementById("fixedThead");
	var rowSize = tbl.rows.length;
	var row = tbl.insertRow(rowSize);
	var cell0 = row.insertCell(0);
	var cell1 = row.insertCell(1);
	var cell2 = row.insertCell(2);
	var cell3 = row.insertCell(3);
	var cell4 = row.insertCell(4);
	cell0.innerHTML = "<input type='checkbox' name='checkLines' style='width:20px'/>";
	cell1.innerHTML = seq;
	cell2.innerHTML = "<input name='lookupCode_" + seq + "' id='lookupCode_"
			+ seq + "' style='width:150px'/>"
	cell3.innerHTML = "<input name='lookupName_" + seq + "' id='lookupName_"
			+ seq + "' style='width:150px'/>"
	cell4.innerHTML = "<input name='remark_" + seq + "' id='remark_" + seq
			+ "' style='width:200px'/>";
	// if (page == 'edit') {
	// // 修改
	// }
	var index = document.getElementById("fixedThead")
			.getElementsByTagName('tr').length;
	seq++;
}
// 删除行
function deleteLine() {
	var deleteIds = "";
	var ckbs = $("input[name='checkLines']:checked");
	if (ckbs.length == 0) {
		$.messager.alert('删除指定行', '要删除指定行，需选中要删除的行！', 'info');
		return;
	} else {
		ckbs.each(function() {
			if ($(this).val() != "") {
				deleteIds = deleteIds + $(this).val() + ",";
			}
			$(this).parent().parent().remove();
		});
		$("#deleteIds").val(deleteIds);
	}
}

function checkmain() {
	if (!checknull("lookupType", "lookupMeaning")) {
		return false;
	}
	var valueTable = document.getElementById("fixedThead");
	var table_tr = valueTable.getElementsByTagName('tr');
	lookupCodeArr = [];
	for (var i = 1; i < table_tr.length; i++) {
		var inputParam = table_tr[i].getElementsByTagName('input');
		lookupCodeArr[i - 1] = inputParam[1].value;
		if (!checknull(inputParam[1].name, inputParam[2].name)) {
			return false;
		}
	}
	return true;
}
// 新增
function addEntity() {
	if (checkmain()) {
		$.ajax({
			url : webUrl + 'lookup/save.do',
			type : 'post',
			data : {
				param : buildEntity('add')
			}
		}).done(
				function(data) {
					var result = data.bo;
					if (result == 0) {
						$.messager.confirm('提示 ', '保存成功！', function(r) {
							$('.main_panel').load(
									webUrl + "public/page/lookup/query.html");
						});
					} else if (result == -1) {
						$.messager.alert('提示 ', '【快速编码类型(英文编码)】已经存在！', 'info');
					} else {
						$.messager.alert('提示 ', '保存失败！', 'info');
					}
				});
	}
}
// 修改
function updateEntity() {
	if (checkmain()) {
		if (arrRepeat(lookupCodeArr)) {
			$.messager.alert('提示 ', '【快速编码代码(英文或数字)】存在重复代码！', 'info');
			return false;// 重复
		}
		$.ajax({
			url : webUrl + 'lookup/update.do',
			type : 'post',
			data : {
				param : buildEntity('update')
			}
		}).done(
				function(data) {
					var result = data.bo;
					if (result == 0) {
						$.messager.confirm('提示 ', '修改成功！', function(r) {
							$('.main_panel').load(
									webUrl + "public/page/lookup/query.html");
						});
					} else {
						$.messager.alert('提示 ', '修改失败！', 'info');
					}
				});
	}
}
// 构建修改
function buildEntity(oper) {
	var json = {};
	json.sysLookupType = {};
	if (oper == 'update') {
		json.sysLookupType.id = $("#id").val();// 修改用到
	}
	json.sysLookupType.lookupType = $("#lookupType").val();
	json.sysLookupType.lookupMeaning = $("#lookupMeaning").val();
	json.sysLookupType.remark = $("#remark").val();
	json.sysLookupValue = [];
	var valueTable = document.getElementById("fixedThead");
	var table_tr = valueTable.getElementsByTagName('tr');
	for (var i = 1; i < table_tr.length; i++) {
		var inputParam = table_tr[i].getElementsByTagName('input');
		var Values = {
			"lookupType" : $("#lookupType").val(),
			"lookupCode" : inputParam[1].value,
			"lookupName" : inputParam[2].value,
			"remark" : inputParam[3].value
		};
		// if (oper == 'update') {
		// Values.id = inputParam[4].value; // 修改用到
		// }
		json.sysLookupValue.push(Values);
	}
	json = JSON.stringify(json);
	return json;
}
// common end =================================================================
$("ul.tabtit").on("click", function(event) {
	$('.main_panel').load(webUrl + "public/page/lookup/query.html");
});